#!/bin/sh

#echo "请输入检测主机"
#read sgateway
sgateway="47.94.7.16"

echo "检测主机 $sgateway"

delay=5000

echo "延迟不超过 $delay 毫秒"

interval=30

echo "每隔 $interval 秒检查一次"

echo "########################################"

count=0

while :
do
  echo ""

  count=`expr $count + 1`
  echo "第 $count 次检查"
  if [ $count -gt 10 ]; then
    reboot
    exit 0
  fi
  
  echo ""

  # 检测通断
  ping -c 1 $sgateway 1>/dev/null 2>&1
  if [  $? -ne 0  ]; then
    sleep $interval
    continue
  fi
  
  if [ $delay -le 0 ]; then
    count=0
    sleep $interval
    continue
  fi

  # 检测延迟
  network=`ping -c 1 $sgateway | awk 'NR==2 {print $6}' | sed "s/=/ /g" | awk '{print $2}'`
  expr ${network} + 0 1>/dev/null 2>&1
  if [ $? -eq 0 ]; then
    #/etc/init.d/network restart
    :
  else
    value_n=`echo $network | sed "s/\.//g"`
    if [ "$value_n" -lt "$delay" ]; then
      echo "检查结果为：网络通畅"
      echo "网络延迟为 $network 秒"
      echo "########################"
      
      count=0
    else
      #/etc/init.d/network restart
      :
    fi
  fi

  sleep $interval
done
